
const info = require('../../providers/dataAccess').info;

const ToolsService = function () {
    const _getConfig = async (appName) =>{
        let szSql = `
            SELECT * FROM partner_sys.apps A
            INNER JOIN  partner_info.app_configs AC ON A.id = AC.appId
            WHERE appName = ? LIMIT 1
        `;
        let ret = await info.query(szSql, [appName]);
        let res = ret && ret.length ? ret[0] : null;

        if (res && res.windowConfigs){
            res.windowConfigs = JSON.parse(res.windowConfigs);
        }
        if (res && res.tabBarConfigs){
            res.tabBarConfigs = JSON.parse(res.tabBarConfigs);
        }
        return res;
    };

    const _getPages = async (appId) => {
        let szSql = `
            SELECT * FROM app_pages WHERE appId = ?;
        `;
        let res = await info.query(szSql, [appId]);
        for (var i = 0; i < res.length; ++i){
            var p = res[i];
            let szSql = `
                SELECT * FROM app_page_compontents
                WHERE appId = ? AND pageId = ? AND status = 1
                ORDER BY sequence ASC;
            `;
            let ret = await info.query(szSql, [appId, p.id]);
            ret.forEach(function (c) {
                c.configs = JSON.parse(c.configs);
            });
            p.components = ret;
        }

        return res;
    };

    return {
        getConfig: _getConfig,
        getPages: _getPages
    }

}();

module.exports = ToolsService;
